<template>
	<view>
		<u-popup :show="show" @close="close">
			<view class="mainpadding">
				<view class="sanshier xiaohei textcenter">修改昵称</view>
				<view class="" style="position: absolute;top: 30rpx;right: 30rpx;" @click="close">
					<u-icon name="close"></u-icon>
				</view>
				<view class="xiahuaxian flexbetween margin_top6">
					<view class="ershiba xiaohei">昵称</view>
					<input class="xiaohei ershiba textright" type="text" placeholder="请输入昵称" v-model="nickname_text">
				</view>
				<view class="flexcenter margin_top6">
					<view class="quxiaobtn flexcenter margin_right5" @click="close">取消</view>
					<view class="quedbtn flexcenter" @click="nickname = nickname_text; submit()">确认</view>
				</view>
			</view>
		</u-popup>
		<view class="mainpadding ffffff xiahuaxian flexbetween" @click="uploadImg(1,avatar,avatar_text)">
			<view class="xiaohei ershiba">头像</view>
			<view class="flexright">
				<image :src="avatar_text" class="margin_right1" style="border-radius: 50%;width: 100rpx;height: 100rpx;"
					mode=""></image>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		<view class="mainpadding ffffff xiahuaxian flexbetween" @click="show = true">
			<view class="xiaohei ershiba">昵称</view>
			<view class="flexright">
				<view class="xiaohei ershiba margin_right1">{{nickname}}</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		<!-- <view class="mainpadding ffffff xiahuaxian flexbetween" @click="xgmm">
			<view class="xiaohei ershiba">设置密码</view>
			<u-icon name="arrow-right"></u-icon>
		</view>
		<view class="mainpadding ffffff xiahuaxian flexbetween" @click="tzxgbsjh">
			<view class="xiaohei ershiba">手机号</view>
			<view class="flexright">
				<view class="xiaohei ershiba margin_right1">{{userInfo.mobile}}</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view> -->
		<!-- 固定底部 -->
		<view class="gudingdb ffffff mainpadding">
			<view class="bighbtn" @click="outlogin">退出登录</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				show: false,
				avatar: "",
				avatar_text: "",
				nickname: "",
				nickname_text: "",
				userInfo: {},
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			outlogin() {
				httpRequest.modal('退出登录', '请确认是否退出登录', true, (res) => {
					if (res) {
						uni.removeStorageSync('token');
						uni.removeStorageSync('user_id');
						httpRequest.toast("退出登录成功")
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000)
					}
				})
			},
			init() {
				httpRequest.request('/api/user/index', 'GET', {}).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
					this.nickname_text = res.data.nickname
					this.userInfo = res.data
				})
			},
			uploadImg(value, upImgs, upImgs_text) {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.avatar = res.url;
					this.avatar_text = image_item
					this.submit()
				}).catch(err => {});
			},
			submit() {
				this.close()
				httpRequest.request('/api/user/profile', 'GET', {
					avatar: this.avatar,
					nickname: this.nickname
				}).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
					this.nickname_text = res.data.nickname
					this.userInfo = res.data
				})
			},
			close() {
				this.show = false
			},
			// 跳转更换绑定手机号码
			tzxgbsjh() {
				uni.navigateTo({
					url: '/pages_mine/genghuanbdsjh'
				})
			},
			// 跳转修改密码
			xgmm() {
				uni.navigateTo({
					url: '/pages_mine/xiugaimm'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.quedbtn {
		width: 280rpx;
		height: 80rpx;
		background: #F17832;
		border-radius: 39rpx 39rpx 39rpx 39rpx;
		font-size: 28rpx;
		color: #FFFFFF;
		border: 2rpx solid #F17832;
	}

	.quxiaobtn {
		width: 280rpx;
		height: 80rpx;
		background: #FFFFFF;
		border-radius: 39rpx 39rpx 39rpx 39rpx;
		font-size: 28rpx;
		color: #333333;
		border: 2rpx solid #E6E6E6;
	}
</style>